package io.renren.controller;

import io.renren.annotation.Login;
import io.renren.common.utils.Result;
import io.renren.entity.repair.RepairInfoEntity;
import io.renren.service.repair.RepairInfoService;
import io.renren.service.repair.RepairTaskService;
import io.renren.vo.TaskRespVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/task")
@Tag(name = "任务管理页")
public class ApiTaskController {
    @Resource
    private RepairTaskService repairTaskService;
    
    @Resource
    private RepairInfoService repairInfoService;
    
    @Login
    @GetMapping("/getRepairTask")
    public Result<List<TaskRespVO>> getRepairTask(){
        return new Result().ok(repairTaskService.getRepairTask());
    }
    
    @Login
    @PostMapping ("/updateTaskStart")
    public Result updateTaskStart(@RequestParam Long id,@RequestAttribute("userId") Long userId){
        repairTaskService.updateById(id,userId);
        return new Result();
    }
    
    @Login
    @PostMapping ("/updataTaskFinish")
    public Result updataTaskFinish(@RequestParam Long id){
        repairTaskService.updateByIdFinish(id);
        return new Result();
    }

    /**
     * 维修人员获取工单详情（不需要userId验证）
     * @param id 工单ID
     * @return 工单详情信息
     */
    @GetMapping("getRootInfoDetail")
    @Login
    public Result getRootInfoDetail(@RequestParam("id") Long id) {
        try {
            // 直接通过ID获取工单详情，不验证所属用户
            RepairInfoEntity detail = repairInfoService.getById(id);
            
            if (detail == null) {
                return new Result().error("未找到该工单");
            }
            
            return new Result().ok(detail);
        } catch (Exception e) {
            return new Result().error("获取工单详情失败：" + e.getMessage());
        }
    }
}
